// pages/subrogation/marginFlow/index.js
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '信用保证金流水',
            'color': true,
            'class': false
        },
        date: "自定义",
        days: 30,
        dateSelector: app.MultipleSelectDate,
        waterType: 1,
        dialogOne: false,
        dialogTwo: false,
        dialogThree: false,
        pageH: app.globalData.pageHeight,
        pageNum: 1,
        picUrl: app.imagesURL,
        hint: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            waterType: options.index ? options.index : 1
        })
        this.formatTime(30)
    },
    /* 获取流水数据 扣款、充值、回款流水 */
    flowData() {
        var that = this,
            pageNum = 1,
            list = [];
        var data = {
            "endDate": that.data.endTime,
            "pageNum": that.data.pageNum,
            "pageSize": 15,
            "salesmanId": wx.getStorageSync('userInfo').id,
            "startDate": that.data.startTime,
            "type": that.data.waterType,
            "orderNo": that.data.goodsName ? that.data.goodsName : ''
        }
        // /bdservice/bdb/WorkerBee/flowData
        app.basePost(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'flowData'
        }), data, res => {
            pageNum = res.data.pageNum;
            if (pageNum == 1) {
                console.log(res.data.flowDataList)
                list = res.data.flowDataList;
            } else {
                list = app.SplitArray(res.data.flowDataList ? res.data.flowDataList : [], that.data.blotter) //分页合并数组
            }
            that.setData({
                blotter: list
            })
        }, (res) => {})
    },
    /* 扣款详情 */
    income(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/DebitDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'DebitDetails',
            q: {
                id: e.currentTarget.dataset.id
            }
        }), (res) => {}, res => {
            console.log(res)
            that.setData({
                dialogList: res.data,
                dialogThree: true,
                slide: true
            })
        })
    },
    /* 充值详情 */
    fine(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/RechargeDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'RechargeDetails',
            q: {
                id: e.currentTarget.dataset.id
            }
        }), (res) => {}, res => {
            that.setData({
                recharge: res.data,
                dialogOne: true
            })
        })
    },
    /* 回款详情 */
    refund(e) {
        var that = this;
        // /bdservice/bdb/WorkerBee/PaymentDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'PaymentDetails',
            q: {
                id: e.currentTarget.dataset.id
            }
        }), (res) => {
            console.log(res)
        }, res => {
            console.log(res)
            that.setData({
                payment: res.data,
                dialogTwo: true
            })
        })
    },
    // 滚动到底部加载更多
    waterLower() {
        var pageNum = Number(this.data.pageNum) + 1;
        this.setData({
            pageNum: pageNum,
            hint: false
        })
        this.flowData()
    },
    handleWater(e) {
        var itemIndex = e.currentTarget.dataset['index']
        this.setData({
            waterType: itemIndex,
            pageNum: 1,
            blotter: [],
        })
        this.flowData()
    },
    handleDate: function (e) {
        let index = e.currentTarget.dataset.item;
        this.setData({
            days: Number(index),
            blotter: [],
            pageNum: 1
        })
        this.formatTime(index)
    },
    bindMultiPickerChange: function (e) {
        var date = e.detail.value
        var startTime = this.data.dateSelector[0][e.detail.value[0]] + '-' + this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]]
        var endTime = this.data.dateSelector[3][e.detail.value[3]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]]
        this.setData({
            date: this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]],
            multiIndex: e.detail.value,
            days: '',
            startTime: startTime + ' 00:00:00',
            endTime: endTime + ' 00:00:00'
        })
        this.flowData()
        console.log(this.data.startTime)
        console.log(this.data.endTime)
    },
    //时间格式化
    formatTime: function (index, date) {
        var date = new Date().getTime();
        var onDate = new Date(date + 86400000);
        var preDate = new Date(date - (index * 86400000)); //前n天
        this.setData({
            startTime: app.formatdata('yyyy-MM-dd 00:00:00', index != 0 ? preDate : new Date()),
            endTime: app.formatdata('yyyy-MM-dd 00:00:00', onDate)
        })
        this.flowData()
    },

    //充值成功
    dialogOne() {
        this.setData({
            dialogOne: this.data.dialogOne ? false : true
        })
    },
    //回款详情
    dialogTwo() {
        this.setData({
            dialogTwo: this.data.dialogTwo ? false : true
        })
    },
    //扣款详情
    dialogThree() {
        this.setData({
            dialogThree: this.data.dialogThree ? false : true
        })
    },
    //跳转搜索
    goSearch() {
        wx.navigateTo({
            url: './search/index',
        })
    },
    slide(){
        this.setData({
            slide: false
        })
    }
})